<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/shop/include :: header"></head>
<body>
<form class="layui-form" action="" lay-filter="shop_supply_goods_warehouse-add-form" style="margin-top: 20px;">
    <div style="display: flex">
        <input type="hidden" id="id" name="id" th:value="${shopSupplyGoodsWarehouse.id}" readonly>
        <input type="hidden" id="supplyId" name="supplyId" th:value="${shopSupplyGoodsWarehouse.supplyId}" readonly>
        <!--  -->
        <div class="layui-form-item" style="display: contents">
            <label class="layui-form-label">展示图：</label>
            <div class="layui-upload-list" id="test1">
                <img class="layui-upload-img" id="demo1" th:src="${shopSupplyGoodsWarehouse.showImage}"
                     style="width: 300px;height: 300px">
                <input type="hidden" name="showImage" id="showImage" th:value="${shopSupplyGoodsWarehouse.showImage}">
            </div>
        </div>
        <div style="width: 63%">
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">商品编码：</label>
                <div class="layui-input-block">
                    <input id="goodsCode" name="goodsCode" placeholder="请输入商品编码"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.goodsCode}" readonly>
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">属性名称：</label>
                <div class="layui-input-block">
                    <input id="name" name="name" placeholder="请输入属性名称"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.name}">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">成本价：</label>
                <div class="layui-input-block">
                    <input id="costPrice" name="costPrice" placeholder="请输入成本价"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.costPrice}">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">售价：</label>
                <div class="layui-input-block">
                    <input id="salePrice" name="salePrice" placeholder="请输入售价"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.salePrice}">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">总库存：</label>
                <div class="layui-input-block">
                    <input id="totalInventory" name="totalInventory" placeholder="请输入总库存"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.totalInventory}">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">预售库存：</label>
                <div class="layui-input-block">
                    <input id="bookInveentory" name="bookInveentory" placeholder="请输入预售库存"
                           class="layui-input" lay-verify="required" autocomplete="off"
                           th:value="${shopSupplyGoodsWarehouse.bookInveentory}">
                </div>
            </div>
        </div>
    </div>
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">属性规格json：</label>
        <div class="layui-input-block" id="specificationsJsons">
        </div>
        <input type="hidden" id="specificationsJson" th:value="${shopSupplyGoodsWarehouse.specificationsJson}">
    </div>
    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="shop_supply_goods_warehouse-add">立即提交</button>
        </div>
    </div>
</form>
<div th:include="/shop/include::footer"></div>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['layer', 'jquery', 'form', 'laydate', 'upload'], function () {
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var form = layui.form;
        var upload = layui.upload;

        //先查询规格json

        $.ajax({
            url: '/platform/goodsspecifications/selectGoodsSpecificationsByGoodsCode?goodsCode=' + $("#goodsCode").val() + '&type=0',
            type: "get",
            success: function (result) {
                var arr = result.data;
                for (var i = 0; i < arr.length; i++) {
                    var str = "";
                    if (arr[i].type == 1) {
                        var arr1 = arr[i].list;
                        var opt = "<option value=\"\">" + "请选择" + "</option>";
                        if (arr1.length == 0) {
                            str += "\t\t\t\t\t\t<div class='layui-w100' style=\"display: flex;margin-bottom: 15px\">\n" +
                                "\t\t\t\t\t\t\t<input id=\"key" + i + "\" class=\"layui-input\" style=\"width: 50%\" value=\"" + arr[i].specificationsName + "\" readonly>\n" +
                                "\t\t\t\t\t\t\t<select id=\"value" + i + "\" style=\"width: 50%\">" + opt + "</select>\n" +
                                "\t\t\t\t\t\t</div>";
                        } else {
                            var value = getValue(arr[i].specificationsName);
                            for (var l = 0; l < arr1.length; l++) {
                                if (value == arr1[l].warehouseName){
                                    opt += "<option selected value=\"" + arr1[l].warehouseName + "\">" + arr1[l].warehouseName + "</option>";
                                }else {
                                    opt += "<option value=\"" + arr1[l].warehouseName + "\">" + arr1[l].warehouseName + "</option>";
                                }
                            }
                            str += "\t\t\t\t\t\t<div class='layui-w100' style=\"display: flex;margin-bottom: 15px\">\n" +
                                "\t\t\t\t\t\t\t<input id=\"key" + i + "\" class=\"layui-input\" style=\"width: 50%\" value=\"" + arr[i].specificationsName + "\" readonly>\n" +
                                "\t\t\t\t\t\t\t<select id=\"value" + i + "\" style=\"width: 50%\">" + opt + "</select>\n" +
                                "\t\t\t\t\t\t</div>";
                        }
                    } else {
                        str += "\t\t\t\t\t\t<div style=\"display: flex;margin-bottom: 15px\">\n" +
                            "\t\t\t\t\t\t\t<input id=\"key" + i + "\" class=\"layui-input\" style=\"width: 50%\" value=\"" + arr[i].specificationsName + "\" readonly>\n" +
                            "\t\t\t\t\t\t\t<input id=\"value" + i + "\" class=\"layui-input\" style=\"width: 50%\" lay-verify=\"required\" value=\"" +getValue(arr[i].specificationsName) + "\" placeholder=\"请输入" + arr[i].specificationsName + "\">\n" +
                            "\t\t\t\t\t\t</div>";
                    }
                    $("#specificationsJsons").append(str)
                    form.render('select');
                }

            },
            error: function (result) {
                layer.msg("查询分类失败")
            }
        });

        var uploadInst1 = upload.render({
            elem: '#test1'
            , url: '/platform/UploadFile' //改成您自己的上传接口
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.status != "SUCCESS") {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#showImage").val(res.data)
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst1.upload();
                });
            }
        });

        function getValue(twoKey) {
            var obj = JSON.parse($("#specificationsJson").val());
            for (var oneObj in obj) {
                var one = obj[oneObj];
                for (var key in one) {
                    if (key == twoKey) {
                        var two = one[key]
                        return two["value"]
                    }
                }
            }
        }

        //监听提交
        form.on('submit(shop_supply_goods_warehouse-add)', function (data) {
            var childrens = $("#specificationsJsons").children();
            var json = "";
            for (var i = 0; i < childrens.length; i++) {
                if ($("#value" + i).val() == undefined || $("#value" + i).val() == null || $("#value" + i).val() == "") {
                    layer.msg("请输入或选择'" + $("#key" + i).val() + "'")
                    return false
                }
                if (i < childrens.length - 1) {
                    json += $("#key" + i).val() + ":" + $("#value" + i).val() + ","
                } else {
                    json += $("#key" + i).val() + ":" + $("#value" + i).val()
                }
            }
            data.field.specificationsJson = json;
            //提交数据data.field
            $.ajax({
                url: '/platform/supplygoodswarehouse/edit',
                type: "post",
                data: JSON.stringify(data.field),
                contentType: 'application/json; charset=utf-8', // 很重要
                async: false,
                success: function (result) {
                    if (result.status == "SUCCESS") {
                        layer.msg(result.message, {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            window.parent.location.reload();
                        });
                    } else {
                        layer.msg(result.message)
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
<style>
    .layui-w100 .layui-form-select .layui-input {
        padding-right: 62px !important;
        width: 100%
    }

    .layui-unselect {
        width: 50%;
    }
</style>
</html>